package com.powerrich.webmagic.PageProcessor;

import com.powerrich.webmagic.Pipeline.CreditPipeline;
import com.powerrich.webmagic.pojo.Credit;
import com.powerrich.webmagic.service.CreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.logging.Logger;

/**
 *
 */
@Component
public class CreditPageProcessor implements PageProcessor {

    @Autowired
    CreditService creditService;

    // 初始地址, 褐言喜欢的音乐id 148174530
    //public static final String START_URL = "https://www.creditchina.gov.cn/biaozhunguifan/zonghexingbiaozhunguifan/201904/t20190404_151843.html";

    //public static final String START_URL = "https://www.baidu.com/";

    public static final String START_URL = "https://www.creditchina.gov.cn";

    private Site site = Site.me()
         //   .setDomain("https://www.creditchina.gov.cn")
            .setSleepTime(1000)
            .setRetryTimes(30)
            .setTimeOut(30000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36")
            .addHeader("Referer","https://www.creditchina.gov.cn/biaozhunguifan/?navPage=3")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
            .addHeader("Accept-Encoding","gzip, deflate, br")
            .addHeader("Accept-Language","zh-CN,zh;q=0.9")
            .addHeader("Cache-Control","max-age=0")
            .addHeader("Connection","keep-alive")
            .addHeader("Upgrade-Insecure-Requests","1")
            .addHeader("Host","www.creditchina.gov.cn")
            .addCookie("Cookie","FSSBBIl1UgzbN7N443S=yM5kp6t7mtPUfBHLzmn9.aghDjAkE9_9Lva0B.V3PsyuATfbd6cDKi5kEWLpi0bM; __jsluid=e440dcbc2a2b3f951b101be805a55e5f; insert1_cookie=81361354; Hm_lvt_0076fef7e919d8d7b24383dc8f1c852a=1556957048,1556980428; browseHistory=%5B%7B%22name%22%3A%22%E5%8C%97%E4%BA%AC%E9%9A%8F%E6%89%8B%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%22%2C%22encryStr%22%3A%22dGZ5ZzQsaTF5%5Cn%22%7D%2C%7B%22name%22%3A%22%E6%B7%B1%E5%9C%B3%E5%B8%82%E9%9A%8F%E6%89%8B%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%22%2C%22encryStr%22%3A%22ejtRbEFBczZBZg%3D%3D%5Cn%22%7D%5D; Hm_lpvt_0076fef7e919d8d7b24383dc8f1c852a=1556980896; FSSBBIl1UgzbN7N443T=30WPy1VytVAhbJwW_uM7lY.uNVILdWOmwsTEBQYpbp0LaPxr5B8zVhSXKP77GqLeL9pyGVkyilCCBsfPbDKQ.3GES1uBE3Xm.DttrDfSKl7W_y1RRfy7N6seHztxzO5r4LJbqBXkdaQSGirQEHX2OPW9bp83LmX1rqPLEXuBalpORMyhqYN58QbD4MWPZZoGUzHAKp2Jef6SRfuwM7Gz6IDfaeXuBiN68BX_lNt.C7OjlJ0koOJm0epBHvAuTuIOW.dUPb0G4Am8uBMozy7SJNKw.Oywdojuk9VS2EmOy3hFPldleal6sI98RUINAkaGqVDZ");

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        System.out.println("**************************************************"+page.getHtml().toString());
        //定义如何抽取页面信息，并保存下来
        Credit credit = new Credit();
        String url = page.getUrl().toString();
        credit.setTitle(page.getHtml().xpath("//*[@class='artical_caption_wrap']/h2/text()").toString());
        System.out.println("**************************************************");
        System.out.println("--"+credit.toString());
        System.out.println("**************************************************");


    }

    public void start(CreditPageProcessor processor, CreditPipeline creditPipeline) {

        long start = System.currentTimeMillis();
        Spider.create(processor)
                //从"https://github.com/code4craft"开始抓
                .addUrl(START_URL)
//              .addPipeline(creditPipeline)
                //开启5个线程抓取
                .thread(1)
                //启动爬虫
                .run();
        long end = System.currentTimeMillis();
        System.out.println("爬虫结束,耗时--->" + (end - start));

    }
}
